package com.example.rbacmanage.util;

import com.alibaba.druid.util.StringUtils;
import com.example.rbacmanage.exception.MyException;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
public class SensitiveWordUtil {

    public static void sensitive(String body) throws MyException {
        SensitiveWordBs sensitiveWordBs = SpringUtils.getBean("sensitiveWordBs");
        if ( !StringUtils.isEmpty(body) && sensitiveWordBs.contains(body)) {
            for (String str:sensitiveWordBs.findAll(body)){
                //检测是否包含过敏文字
                if (!sensitiveWordBs.contains(str)){
                    continue;
                }
                //排除数字
                if (str.matches("-?\\d+(\\.\\d+)?")){
                    continue;
                }
                //排除图片
                if (str.contains("jpg") || str.contains("jpeg") || str.contains("png") || str.contains("gif")){
                    continue;
                }
                //排除域名
                if (str.contains("api.springautumncome.asia") || str.contains("https://back.springautumncome.asia")){
                    continue;
                }
                throw new MyException("包含敏感词汇'"+ SensitiveWordHelper.findFirst(str)+"'，请重新输入",512);
            }
        }
    }
}
